package com.example.springboot.service;

import cn.hutool.core.util.StrUtil;
import com.example.springboot.entity.DrugClassification;
import com.example.springboot.entity.DrugDetails;
import com.example.springboot.entity.User;
import com.example.springboot.exception.CustomerException;
import com.example.springboot.mapper.DrugMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DrugService {

    @Resource
    DrugMapper drugMapper;

    public PageInfo<DrugClassification> selectDrugClassificationPage(Integer pageNum, Integer pageSize, DrugClassification drugClassification) {
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<DrugClassification> drugClassificationList = drugMapper.selectDrugClassificationAll(drugClassification);
        return PageInfo.of(drugClassificationList);
    }

    public void addDrugClassification(DrugClassification drugClassification) {
        DrugClassification dbdrugClassification = drugMapper.selectByDrugClassificationName(drugClassification.getName());
        if (dbdrugClassification != null){
            throw new CustomerException("该分类已存在");
        }
        drugMapper.insertDrugClassification(drugClassification);
    }

    public void updateDrugClassification(DrugClassification drugClassification) {
        drugMapper.updateDrugClassificationById(drugClassification);
    }

    public void deleteDrugClassificationById(Integer id) {
        drugMapper.deleteDrugClassificationById(id);
    }

    public void deleteBatchDrugClassification(List<DrugClassification> drugClassificationList) {
        for (DrugClassification drugClassification : drugClassificationList) {
            this.deleteDrugClassificationById(drugClassification.getId());
        }
    }

    public PageInfo<DrugDetails> selectDrugDetailsPage(Integer pageNum, Integer pageSize, DrugDetails drugDetails) {
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<DrugDetails> drugDetailsList = drugMapper.selectDrugDetailsAll(drugDetails);
        return PageInfo.of(drugDetailsList);
    }

    public void addDrugDetails(DrugDetails drugDetails) {
        DrugDetails dbDrugDetails = drugMapper.selectByDrugDetails(drugDetails.getName());
        if (dbDrugDetails != null){
            throw new CustomerException("该名称药品已存在");
        }
        drugMapper.insertDrugDetails(drugDetails);
    }

    public List<DrugDetails> selectDrugDetailsAll() {
        return drugMapper.selectDrugDetailsAll(null);
    }

    public List<DrugClassification> selectDrugClassificationAll() {
        return drugMapper.selectDrugClassificationAll(null);
    }

    public void updateDrugDetails(DrugDetails drugDetails) {
        drugMapper.updateDrugDetails(drugDetails);
    }
}
